package pzl.com.androidloader;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import java.util.List;
import java.util.Random;

import pzl.com.androidloader.beans.Bread;
import pzl.com.androidloader.broadcastreceiver.Bakery;
import pzl.com.androidloader.loader.Baker;

public class Demo2Activity extends AppCompatActivity implements Baker.BakeryCallback {

    private LoaderManager.LoaderCallbacks<List<Bread>> mCallbacks;
    //面包房
    private Bakery mBakery;
    //面包师
    private Baker mBaker;
    //面包需求量
    private int mNeededBreads;
    //唯一标识
    private final int mLoaderId = 22;

    private LinearLayout ll;
    private ScrollView sv;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_demo2);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        ll = (LinearLayout) findViewById(R.id.ll);
        sv = (ScrollView) findViewById(R.id.sv);
        setSupportActionBar(toolbar);

        mBaker = new Baker(this, this);
        mBakery = new Bakery(mBaker);
        mCallbacks = new LoaderManager.LoaderCallbacks<List<Bread>>() {

            @Override
            public Loader<List<Bread>> onCreateLoader(int id, Bundle args) {
                if (mBaker == null) {
                    mBaker = new Baker(Demo2Activity.this, Demo2Activity.this);
                }
                return mBaker;
            }

            @Override
            public void onLoadFinished(Loader<List<Bread>> loader, List<Bread> data) {
                mNeededBreads = 0;
                //面包师完成面包烤制
                TextView tv = new TextView(loader.getContext());
                tv.setText("scott sell " + data.size() + " breads");
                tv.setGravity(Gravity.CENTER);
                tv.setTextColor(Color.BLACK);
                tv.setTextSize(33);
                LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                ll.addView(tv, lp);//android 大多数函数是基于消息队列同步的，调用函数后立即返回所以是异步的，一些不到位的顺序问题post到消息队列
                sv.post(new Runnable() {
                    @Override
                    public void run() {
                        sv.fullScroll(View.FOCUS_DOWN);
                    }
                });
            }

            @Override
            public void onLoaderReset(Loader<List<Bread>> loader) {

            }
        };

        //面包师开始工作
        getSupportLoaderManager().restartLoader(mLoaderId, null, mCallbacks);
        mockCustomer();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        getApplication().getApplicationContext().unregisterReceiver(mBakery);
    }

    @Override
    public int getNeededBreads() {
        return mNeededBreads;
    }

    //模拟源源不断的顾客需求
    private void mockCustomer() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                while (true) {
                    try {
                        Thread.sleep(3000);
                        Random random = new Random();
                        mNeededBreads = random.nextInt(10);
                        Intent intent = new Intent(Bakery.ACTION_RECEIVER_BAKERY);
                        sendBroadcast(intent);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
}
